from pyspark import SparkContext

# old = 
sc = SparkContext('local')
old = sc.parallelize(range(10))

samplePartition = [old.sample(withReplacement=True, fraction=0.5) for i in range(5)]

for num, element in zip(range(len(samplePartition)), samplePartition):
    print("sample: %s y=%s" %(str(num),str(element.collect())))

takeSamplePartition = [old.takeSample(withReplacement=True, num=3) for i in range(5)]

for num, element in zip(range(len(takeSamplePartition)), takeSamplePartition):
    print('takeSample: %s y=%s' %(str(num), str(element)))


dataList = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
old2 = sc.parallelize(dataList)
samplePartition2 = [old2.sampleByKey(withReplacement=False, fractions={'a':0.1, 'b':0.3, 'c':0.2, 'd':0.3, 'e': 0.1}) for i in range(5)]

for num, element in zip(range(len(samplePartition2)), samplePartition2):
    print("sampleByKey: %s y=%s" %(str(num), str(element.collect())))
# y = [old2.sampleByKey(withReplacement=False,
#                         fractions={'A':0.5, 'B':1, 'C':0.2, 'D':0.6, 'E':0.8}) for i in range(5)]

print('==================')

# sc = SparkContext('local')
# old=sc.parallelize(range(8))
# samplePartition = [old.sample(withReplacement=True, fraction=0.5) for i in range(5)]
# for num, element in zip(range(len(samplePartition)), samplePartition) :
#     print('sample: %s y=%s' %(str(num),str(element.collect())))
# taskSamplePartition  = [old.takeSample(withReplacement=False, num=4) for i in range(5)]
# for num, element in zip(range(len(taskSamplePartition)), taskSamplePartition) :
#     #注意因为是action，所以element是集合对象，而不是rdd的分区
#     print('taskSample: %s y=%s' %(str(num),str(element)))
# mapRdd = sc.parallelize([('B',1),('A',2),('C',3),('D',4),('E',5)])
# y = [mapRdd.sampleByKey(withReplacement=False,
#                         fractions={'A':0.5, 'B':1, 'C':0.2, 'D':0.6, 'E':0.8}) for i in range(5)]
# for num, element in zip(range(len(y)), y) :
#     #注意因为是action，所以element是集合对象，而不是rdd的分区
#     print('y: %s y=%s' %(str(num),str(element.collect())))

